/***************************************************************************
    QRSAGSExtentGroupBox.cpp
    ---------------------
    begin                : March 2014
    copyright            : (C) 2014 by Martin Dobias
    email                : wonder dot sk at gmail dot com
 ***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "qrsagsextentgroupbox.h"
#include "QRSAGSExtentWidget.h"

QRSAGSExtentGroupBox::QRSAGSExtentGroupBox( QWidget *parent )
  : QgsCollapsibleGroupBox( parent )
  , mTitleBase( tr( "Extent" ) )
{
  mWidget = new QRSAGSExtentWidget( nullptr, QRSAGSExtentWidget::ExpandedStyle );
  QVBoxLayout *layout = new QVBoxLayout();
  layout->addWidget( mWidget );
  setLayout( layout );

  connect( this, &QGroupBox::toggled, this, &QRSAGSExtentGroupBox::groupBoxClicked );
  connect( mWidget, &QRSAGSExtentWidget::extentChanged, this, &QRSAGSExtentGroupBox::widgetExtentChanged );
  connect( mWidget, &QRSAGSExtentWidget::validationChanged, this, &QRSAGSExtentGroupBox::validationChanged );

  connect( mWidget, &QRSAGSExtentWidget::toggleDialogVisibility, this, [ = ]( bool visible )
  {
    window()->setVisible( visible );
  } );
}

void QRSAGSExtentGroupBox::setOriginalExtent( const QgsRectangle &originalExtent, const QgsCoordinateReferenceSystem &originalCrs )
{
  mWidget->setOriginalExtent( originalExtent, originalCrs );
}

QgsRectangle QRSAGSExtentGroupBox::originalExtent() const
{
  return mWidget->originalExtent();
}

QgsCoordinateReferenceSystem QRSAGSExtentGroupBox::originalCrs() const
{
  return mWidget->originalCrs();
}

void QRSAGSExtentGroupBox::setCurrentExtent( const QgsRectangle &currentExtent, const QgsCoordinateReferenceSystem &currentCrs )
{
  mWidget->setCurrentExtent( currentExtent, currentCrs );
}

QgsRectangle QRSAGSExtentGroupBox::currentExtent() const
{
  return mWidget->currentExtent();
}

QgsCoordinateReferenceSystem QRSAGSExtentGroupBox::currentCrs() const
{
  return mWidget->currentCrs();
}

void QRSAGSExtentGroupBox::setOutputCrs( const QgsCoordinateReferenceSystem &outputCrs )
{
  mWidget->setOutputCrs( outputCrs );
}

void QRSAGSExtentGroupBox::updateTitle()
{
  QString msg;
  switch ( mWidget->extentState() )
  {
    case QRSAGSExtentWidget::OriginalExtent:
      msg = tr( "layer" );
      break;
    case QRSAGSExtentWidget::CurrentExtent:
      msg = tr( "map view" );
      break;
    case QRSAGSExtentWidget::UserExtent:
      msg = tr( "user defined" );
      break;
    case QRSAGSExtentWidget::ProjectLayerExtent:
      msg = mWidget->extentLayerName();
      break;
    case QRSAGSExtentWidget::DrawOnCanvas:
      msg = tr( "drawn on canvas" );
      break;
  }
  if ( isCheckable() && !isChecked() )
    msg = tr( "none" );
  msg = tr( "%1 (current: %2)" ).arg( mTitleBase, msg );

  setTitle( msg );
}

void QRSAGSExtentGroupBox::setOutputExtentFromCurrent()
{
  mWidget->setOutputExtentFromCurrent();
}

void QRSAGSExtentGroupBox::setOutputExtentFromOriginal()
{
  mWidget->setOutputExtentFromOriginal();
}

void QRSAGSExtentGroupBox::setOutputExtentFromUser( const QgsRectangle &extent, const QgsCoordinateReferenceSystem &crs )
{
  mWidget->setOutputExtentFromUser( extent, crs );
}

void QRSAGSExtentGroupBox::setOutputExtentFromLayer( const QgsMapLayer *layer )
{
  mWidget->setOutputExtentFromLayer( layer );
}

void QRSAGSExtentGroupBox::setOutputExtentFromDrawOnCanvas()
{
  mWidget->setOutputExtentFromDrawOnCanvas();
}

void QRSAGSExtentGroupBox::setRatio( QSize ratio )
{
  mWidget->setRatio( ratio );
}

void QRSAGSExtentGroupBox::groupBoxClicked()
{
  if ( !isCheckable() )
    return;

  updateTitle();

  // output extent just went from null to something (or vice versa)
  emit extentChanged( outputExtent() );
}

void QRSAGSExtentGroupBox::widgetExtentChanged()
{
  updateTitle();

  emit extentChanged( outputExtent() );
}

void QRSAGSExtentGroupBox::validationChanged( bool valid )
{
  if ( valid )
  {
    if ( isCheckable() && !isChecked() )
      setChecked( true );
  }
  else if ( isCheckable() && isChecked() )
    setChecked( false );
}

QgsRectangle QRSAGSExtentGroupBox::outputExtent() const
{
  if ( isCheckable() && !isChecked() )
    return QgsRectangle();

  return mWidget->outputExtent();
}

QgsCoordinateReferenceSystem QRSAGSExtentGroupBox::outputCrs() const
{
  return mWidget->outputCrs();
}

QRSAGSExtentGroupBox::ExtentState QRSAGSExtentGroupBox::extentState() const
{
  return static_cast< QRSAGSExtentGroupBox::ExtentState >( mWidget->extentState() );
}

void QRSAGSExtentGroupBox::setTitleBase( const QString &title )
{
  mTitleBase = title;
  updateTitle();
}

QString QRSAGSExtentGroupBox::titleBase() const
{
  return mTitleBase;
}

void QRSAGSExtentGroupBox::setMapCanvas( QgsMapCanvas *canvas )
{
  mWidget->setMapCanvas( canvas );
}

QSize QRSAGSExtentGroupBox::ratio() const
{
    return mWidget->ratio();
}

void QRSAGSExtentGroupBox::clearExtentRubberBand()
{
    mWidget->clearExtentRubberBand();
}
